import 'package:flutter/material.dart';
import 'package:flutter_test_qq/updatePwd.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'messagePage.dart';
import 'dynamicPage.dart';
import 'contactsPage.dart';
import 'smallWorld.dart';
import 'myPage.dart';
class mainPage extends StatefulWidget{

  @override
  mainPageState createState() => mainPageState();
}


class mainPageState extends State<mainPage>{
  var allPages=[messagePage(),contactsPage(),smallWorld(),dynamicPage()];
  var currentIndex=0;
  var Name = '';
  var qqpic = ''; // qq头像
  List itemsList=[
    {
      "icon":"images/Icons/1.png",
      "title":'直播',
    },
    {
      "icon":"images/Icons/2.png",
      "title":'我的QQ钱包',
    },
    {
      "icon":"images/Icons/3.png",
      "title":'装扮我的QQ',
    },
    {
      "icon":"images/Icons/4.png",
      "title":'我的收藏',
    },
    {
      "icon":"images/Icons/5.png",
      "title":'我的相册',
    },
    {
      "icon":"images/Icons/6.png",
      "title":'我的文件',
    },
  ];
  get() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Name = prefs.getString("Name").toString();
    qqpic = prefs.getString("Pic").toString();
    print('qqpic:${qqpic}');
  }
  @override
  void initState() {
    // TODO: implement initState
    get();
    super.initState();
  }
  @override
  Widget build(BuildContext context){
    return Theme(
        data: ThemeData(
          primarySwatch: Colors.lightGreen,
          iconTheme: IconThemeData(color: Colors.lightGreen)
        ),
        child:  Scaffold(
            body: allPages[currentIndex],
            backgroundColor: Colors.white,
            appBar: AppBar(
              // 头像
              // backgroundColor:Colors.greenAccent,
              leading: IconButton(
                icon: Container(
                  padding: EdgeInsets.all(1.0),
                  child: CircleAvatar(backgroundImage: AssetImage(qqpic)),
                ),
                onPressed: (){
                  Navigator.of(context)
                      .push(MaterialPageRoute(builder: (BuildContext context) {
                    return updatePwd();
                  }));
                },
              ),
              // 昵称
              title: Text(Name,style: TextStyle(
                fontSize: 17,
              ),),
              // 右方菜单
              actions: [
                PopupMenuButton(
                    child: Container(
                      margin: const EdgeInsets.all(5),
                      child: Icon(Icons.add),
                    ),
                    itemBuilder: (context)=>[
                      PopupMenuItem(
                        child: const Text('创建群聊'),
                        value: 1,
                        onTap: (){},
                      ),
                      PopupMenuItem(
                        child: const Text('加好友/群'),
                        value: 2,
                        onTap: (){},
                      ),
                      PopupMenuItem(
                        child: const Text('扫一扫'),
                        value: 3,
                        onTap: (){},
                      ),
                      PopupMenuItem(
                        child: const Text('面对面快传'),
                        value: 4,
                        onTap: (){},
                      ),
                      PopupMenuItem(
                        child: const Text('收付款'),
                        value: 5,
                        onTap: (){},
                      ),
                    ]
                )
              ],
            ),
            drawer: Drawer(
              child: Column(
                children: [
                  SizedBox(
                    child: Stack(
                      alignment: FractionalOffset(0.5,0.7),
                      children: [
                        Image.asset('images/pic4.jpg',fit: BoxFit.cover,width: 600),
                        Container(
                          color: Colors.black.withOpacity(0.3),
                        ),
                        Positioned(
                          top: 43,
                          left: 15,
                          child: Row(
                            children: [
                              SizedBox(
                                width: 80,
                                height:80,
                                child: CircleAvatar(backgroundImage: AssetImage(this.qqpic)),
                              ),
                              SizedBox(width: 20,),
                              Text((this.Name==null?Name:this.Name),style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 30
                              ),),
                            ],
                          ),
                        ),
                      ],
                    ),
                    width: 420,
                    height: 150,
                  ),
                  Container(
                      width: 350,
                      height: 350,
                      child:  ListView.builder(
                        itemCount: itemsList.length,
                        itemBuilder: this.getData,
                      )
                  ),
                  SizedBox(height:20,),
                  SizedBox(
                    height: 60,
                    width: 300,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        // Text('设置'),
                        SizedBox(
                            child: Row(
                              children: [
                                TextButton(
                                  onPressed:(){
                                    print('进入修改密码页面');
                                    Navigator.of(context)
                                        .push(MaterialPageRoute(builder: (BuildContext context) {
                                      return updatePwd();

                                    }));},
                                  child: Text('修改密码'),
                                ),
                              ],
                            )
                        ),
                        Text('夜间'),
                        Text('等级'),
                        Text('天气'),
                        Text(' ')
                      ],
                    ),
                  ),
                ],
              ),
            ),
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              unselectedItemColor: Colors.grey,
              selectedItemColor: Colors.lightGreen,
              currentIndex: currentIndex,
              items: [
                BottomNavigationBarItem(
                    icon: Icon(Icons.message),
                    label:"消息"
                ),
                BottomNavigationBarItem(
                    icon: Icon(Icons.people),
                    label:"联系人"
                ),
                BottomNavigationBarItem(
                    icon: Icon(Icons.camera),
                    label:"小世界"
                ),
                BottomNavigationBarItem(
                    icon: Icon(Icons.star),
                    label:"动态"
                ),
              ],
              onTap: (index){
                setState(() {
                  print("the index is :$index");
                  currentIndex=index;
                });
              },
            ),
          )
    );
  }

  Widget getData(context,index){
    return ListTile(
      title: Text(itemsList[index]['title']),
      leading: Image(image: AssetImage(itemsList[index]['icon']),width:30,height:30),
      trailing: Icon(Icons.keyboard_arrow_right),
    );
  }
}
